<?php

class Login extends M_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    // 用户信息
    public function userInfo()
    {
        if (isset($this->uid) && $this->uid > 0) {
            $info = $this->models('member')->uidInfo(
                ['uid' => $this->uid],
                'uid,email,salt,password,username,name,avatar,introduce,randcode,real_status,money,team_code'
            );
            $password = '123456';
            if (md5(md5($password) . $info['salt'] . md5($password)) == $info['password']) {
                $status = true;
            } else {
                $status = false;
            }
            unset($info['salt'], $info['password']);
            $info['avatar'] = dr_thumb($info['avatar'], 200, 200);
            apiSuccess(1, 'success', ['info' => $info, 'status' => $status]);
        } else {
            apiSuccess(-4001, '请登录');
        }
    }

    //账密登录
    public function accountLogin()
    {
        try {
            $data = $this->input->post();
            if (empty($_POST['phone'])) {
                throw new Exception('请输入账号');
            }
            $memberInfo = $this->models('member')->uidInfo(['username' => trim($data['phone'])], 'uid,password,salt');
            if (empty($memberInfo)) {
                throw new Exception('账户不存在', 1);
            }
            if ($data['type'] == 'account') {
                $password = trim($_POST['password']);
                if (md5(md5($password) . $memberInfo['salt'] . md5($password)) != $memberInfo['password']) {
                    throw new Exception('密码不正确', 1);
                }
            } elseif ($data['type'] == 'code') {
                $code = $this->models('system/sms')->getCaptcha(trim($data['phone']), 'login');
                if ($code <> $data['code']) {
                    throw new Exception('验证码错误');
                }
                $this->models('system/sms')->deleteCaptcha(trim($data['phone']), 'login');
            } else {
                throw new Exception('登录方式不支持');
            }
            apiSuccess(1, 'success', $this->models('member/login')->authcode($memberInfo['uid']));
        } catch (Exception $e) {
            apiSuccess(0, $e->getMessage());
        }
    }

    // 修改密码
    public function changePassword()
    {
        try {
            $data = $this->input->post();
            if ((int)$this->uid < 1) {
                apiSuccess(-4001, '请登录');
            }
            $member = $this->models('member')->uidInfo(['uid' => $this->uid], 'uid,password,salt');
            if (empty($member)) {
                throw new Exception('用户信息不存在', 1);
            }
            $password = trim($data['password']); // 原密码
            $newPassword = trim($data['new_password']); // 新密码
            $confirmPassword = trim($data['confirmPassword']); // 确认密码
            if (strlen($newPassword) < 3) {
                throw new Exception('密码至少三位');
            }
            if ($confirmPassword != $newPassword) {
                throw  new Exception('两次密码不一致', 1);
            }
            if (md5(md5($password) . $member['salt'] . md5($password)) <> $member['password']) {
                throw new Exception('原密码错误', 1);
            }
            $this->checkCode(trim($data['phone']), 'change_password', $data['code']);
            if (empty($this->models('member')->upMember(
                [
                    'password' => md5(md5(trim($newPassword)) . $member['salt'] . md5(trim($newPassword))),
                ],
                $member['uid']
            ))) {
                throw  new Exception('修改失败', 1);
            }
            $this->models('system/sms')->deleteCaptcha(trim($data['phone']), 'change_password');
            apiSuccess(1, '修改成功', $this->models('member/login')->authcode($member['uid']));
        } catch (Exception $e) {
            if ($e->getCode() == 2) {
                apiSuccess(-4001, $e->getMessage());
            }
            apiSuccess(0, $e->getMessage());
        }
    }

    //忘记密码&& 重置
    public function forgetPassword()
    {
        try {
            if (empty($_POST['phone'])) {
                throw  new Exception('手机号不存在', 1);
            }
            if (trim($_POST['password']) !== trim($_POST['new_psd'])) {
                throw  new Exception('两次密码不一致', 1);
            }
            $code = $this->models('system/sms')->getCaptcha(trim($_POST['phone']), 'find_password');
            if (empty($code)) {
                throw  new Exception('验证码不存在', 1);
            }
            $memberInfo = $this->models('member')->uidInfo(['username' => trim($_POST['phone'])], 'uid,password,salt');
            if (empty($memberInfo)) {
                throw  new Exception('用户不存在', 1);
            }
            ### 更新密码
            $data = [
                'password' => md5(md5(trim($_POST['password'])) . $memberInfo['salt'] . md5(trim($_POST['password']))),
            ];
            if (empty($this->models('member')->upMember($memberInfo['uid'], $data))) {
                throw  new Exception('修改失败', 1);
            }
            $this->models('system/sms')->deleteCaptcha(trim($_POST['phone']), 'find_password');
            x_json(1, $this->models('member/login')->authcode($memberInfo['uid']), '修改成功');
        } catch (Exception $e) {
            x_json(0, $e->getMessage(), false);
        }

    }

    // 修改支付密码
    public function changePayPass()
    {
        try {
            if ((int)$this->uid < 1) {
                apiSuccess(-4001, '请登录');
            }
            $initial = '123456';
            if ((int)$this->uid < 1) {
                apiSuccess(-4001, '请登录');
            }
            $pay = $this->models('member')->memberPayPsd($this->uid);
            if (empty($pay)) {
                throw new Exception('密码不存在');
            }
            if (IS_POST) {
                $data = $this->input->post();
                $this->checkCode(trim($data['phone']), 'change_pay', $data['code']);
                $password = trim($data['password']); // 原密码
                $newPassword = trim($data['new_password']); // 新密码
                if (empty($newPassword)) {
                    throw new Exception('请输入新支付密码');
                }
                if ($initial == $newPassword) {
                    throw new Exception('不能设置与初始密码相同');
                }
                if ($password == $newPassword) {
                    throw new Exception('请修改不同的密码');
                }
                if (md5(md5($password) . $pay['salt'] . md5($password)) <> $pay['password']) {
                    throw new Exception('原密码错误');
                }
                $newPassword = md5(md5($newPassword) . $pay['salt'] . md5($newPassword));
                if (empty($this->models('member')->editPayPassword($this->uid, $newPassword))) {
                    throw new Exception('修改失败');
                }
                apiSuccess(1, '修改成功');
            } else {
                $initialPsd = md5(md5($initial) . $pay['salt'] . md5($initial));
                apiSuccess(1, 'success', ['status' => $initialPsd == $pay['password']]);
            }
        } catch (Exception $e) {
            apiSuccess(0, $e->getMessage());
        }

    }

    /**
     * 验证:验证码
     * @throws Exception
     */
    protected function checkCode($phone, $type, $code)
    {
        $captcha = $this->models('system/sms')->getCaptcha($phone, $type);
        if (empty($captcha)) {
            throw  new Exception('验证码不存在', 1);
        }
        if ($code <> $captcha) {
            throw new Exception('验证码错误');
        }
    }

}